package com.example.safeassistan.risk.index;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseVPFragment;
import com.example.safeassistan.message.activity.Activity_Message;
import com.example.safeassistan.risk.fragment.RiskDepartmentFragment;
import com.example.safeassistan.risk.fragment.RiskMyFragment;

import java.util.ArrayList;
import java.util.List;

public class RiskIndex extends BaseVPFragment implements View.OnClickListener {
    private Context context;
    private View view;
    private ImageView ivMsg;
    private ViewPager viewPager;
    private TextView tvMyRisk,tvDepartmentRisk;
    private List<Fragment> fragmentList;
    private List<String> mTitleList;
    private MainAdapter adapter;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvDepartmentRisk:
                reSetStatus();
                tvDepartmentRisk.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
                tvDepartmentRisk.setTextColor(context.getResources().getColor(R.color.gray3));
                viewPager.setCurrentItem(1);
                break;
            case R.id.tvMyRisk:
                reSetStatus();
                tvMyRisk.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
                tvMyRisk.setTextColor(context.getResources().getColor(R.color.gray3));
                viewPager.setCurrentItem(0);
                break;
            case R.id.ivMsg:
                Activity_Message.actionStart(context);
                break;
        }
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) { }

    @Override
    protected void onFragmentFirstVisible() { }

    public static RiskIndex newInstance(String content) {
        Bundle arguments = new Bundle();
        arguments.putString("string", content);
        RiskIndex fl = new RiskIndex();
        fl.setArguments(arguments);
        return fl;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.risk_index, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    public void initView(){
        ivMsg=view.findViewById(R.id.ivMsg);
        tvMyRisk=view.findViewById(R.id.tvMyRisk);
        tvDepartmentRisk=view.findViewById(R.id.tvDepartmentRisk);
        tvDepartmentRisk.setOnClickListener(this::onClick);
        tvMyRisk.setOnClickListener(this::onClick);
        ivMsg.setOnClickListener(this::onClick);
        viewPager=view.findViewById(R.id.viewPager);

        fragmentList=new ArrayList<>();
        fragmentList.add(RiskMyFragment.newInstance(0));
        fragmentList.add(RiskDepartmentFragment.newInstance(1));
        mTitleList=new ArrayList<>();
        mTitleList.add("我的");
        mTitleList.add("部门");
        adapter=new MainAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                switch (position){
                    case 0:
                        reSetStatus();
                        tvMyRisk.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
                        tvMyRisk.setTextColor(context.getResources().getColor(R.color.gray3));
                        break;
                    case 1:
                        reSetStatus();
                        tvDepartmentRisk.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
                        tvDepartmentRisk.setTextColor(context.getResources().getColor(R.color.gray3));
                        break;
                }
            }

            @Override
            public void onPageSelected(int position) { }

            @Override
            public void onPageScrollStateChanged(int state) { }
        });
    }

    public void reSetStatus(){
        tvMyRisk.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
        tvMyRisk.setTextColor(context.getResources().getColor(R.color.gray9));
        tvDepartmentRisk.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
        tvDepartmentRisk.setTextColor(context.getResources().getColor(R.color.gray9));
    }

    //适配器
    public class MainAdapter extends FragmentPagerAdapter {

        public MainAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        //获取标题
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position);
        }
    }
}